package com.shop.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.common.result.Result;
import com.shop.common.result.util.ResultModel;
import com.shop.entity.CommodityDo;
import com.shop.service.CommodityService;
import com.shop.vo.commodity.AddCommodityVo;
import com.shop.vo.commodity.UpdateCommodityVo;
import com.shop.vo.seller.SelectCommodityByWarehouseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author 靳 旺
 * @since 2020-12-16
 */

@RestController
@Api(tags = "商品前端控制器")
@RequestMapping("/commodity-do")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;



    @PostMapping("update")
    public ResultModel updateCommodity(@RequestBody @Valid UpdateCommodityVo updateCommodityVo, BindingResult result) throws Exception {
        if (result.hasErrors())  return Result.errorResultModel(result.getAllErrors());
        return commodityService.updateCommodity(updateCommodityVo);
    }

    @GetMapping("/list")
    public ResultModel list(@RequestParam Integer page,@RequestParam Integer size) throws Exception {
        return commodityService.getCommodityList(page,size);
    }

    @GetMapping("/search")
    public ResultModel search(@RequestParam String name) throws Exception {
        return commodityService.search(name);
    }

    //Restful风格接口
    @GetMapping(value = "/del/{id}")
    public ResultModel del(@PathVariable("id") Integer id) throws Exception {
        return commodityService.delCommodity(id);
    }


    @PostMapping("add")
    public ResultModel add(@RequestBody @Valid AddCommodityVo addCommodityVo, BindingResult result) throws Exception {
        if (result.hasErrors())  return Result.errorResultModel(result.getAllErrors());
        return commodityService.addCommodity(addCommodityVo);
    }


    @PostMapping("selectCommodityByWarehouse")
    @ApiOperation(value = "查询仓库商品",notes = "靳旺")
    public ResultModel selectCommodityByWarehouse(@RequestBody SelectCommodityByWarehouseVo selectCommodityByWarehouseVo)throws Exception{
        return commodityService.selectCommodityByWarehouse(selectCommodityByWarehouseVo);
    }

}
